package org.dromara.system.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.mq.config.EmailQueueConfig;
import org.dromara.system.service.IEmailQueueService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RabbitListener(queues = EmailQueueConfig.EMAIL_DELAYED_QUEUE)
public class EmailReceiver {

    @Autowired
    private IEmailQueueService queueService;

    @RabbitHandler
    public void receiveMessage(Long queueId, Message message, Channel channel) {
        try {
            queueService.sendEmailQueueMQ(queueId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            log.error("延时发送邮件 {} 异常{},{}", queueId, getClass(), e.getMessage());
        }
    }
}
